<?php

namespace App\Http\Controllers\Merchant;

use App\CommissionConfig;
use App\Config;
use App\Http\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ServiceConfigController extends Controller
{
    use Response;
    public function index(Request $request)
    {
       $data=Config::where([['title','=','service_provider'],['mch_id','=',auth('merchant')->id()]])->first();
       return $this->success(200,$data);
    }
    public function store(Request $request)
    {
        $find=Config::where([['title','=','service_provider'],['mch_id','=',auth('merchant')->id()]])->first();
        if ($find){
            return $this->message(505,trans('topic.create failed'));
        }
        $arr=[
            "title"=>"service_config",
            "value"=>$request->all(),
            'mch_id'=>auth('merchant')->id()
        ];
        $created=Config::create($arr);
        if ($created){
            return $this->message(200,trans('topic.create success'));
        }else{
            return $this->message(505,trans('topic.create failed'));
        }
    }
    public function update(Request $request)
    {
         $update=Config::where([['title','=','service_provider'],['mch_id','=',auth('merchant')->id()]])->update(['value'=>json_encode($request->all())]);
        if ($update){
            return $this->message(200,trans('topic.update success'));
        }else{
            return $this->error(505,trans('topic.update failed'));
        }
    }
}
